package com.neegix.development.menu.application.service.impl;

import com.neegix.application.command.BaseService;
import com.neegix.development.menu.infrastructure.repository.dataobject.MenuDO;
import com.neegix.development.menu.interfaces.vo.MenuVO;
import com.neegix.exception.BusinessRuntimeException;
import com.neegix.development.menu.application.cqrs.command.BatchDeleteMenuCommand;
import com.neegix.development.menu.application.cqrs.command.NewMenuCommand;
import com.neegix.development.menu.application.cqrs.command.UpdateMenuCommand;
import com.neegix.development.menu.application.cqrs.query.MenuQueryRepository;
import com.neegix.development.menu.application.service.MenuService;
import com.neegix.development.menu.domain.entity.MenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-21 16:19:51
  */

@Service
public class MenuServiceImpl extends BaseService implements MenuService{
    @Autowired
    private MenuQueryRepository menuQueryRepository;

    @Override
    public Void createMenu(MenuEntity menuEntity) {
        Optional<MenuVO> optional = menuQueryRepository.findByName(menuEntity.getName());
        // 如果值存在，即菜单已存在，抛出异常
        optional.ifPresent(menuVO -> {
            throw new BusinessRuntimeException("菜单[" + menuEntity.getName() + "]已存在！");
        });
        return commandInvoker.execute(new NewMenuCommand(menuEntity));
    }

    @Override
    public Void modifyMenu(MenuEntity menuEntity) {
        return commandInvoker.execute(new UpdateMenuCommand(menuEntity));
    }

    @Override
    public Void removeMenu(List<Long> ids) {
        Integer count = menuQueryRepository.selectCount(ids);
        if (count.equals(0)){
            throw new BusinessRuntimeException("要删除的记录不存在！");
        }
        return commandInvoker.execute(new BatchDeleteMenuCommand(ids));
    }
}
